$(function () {
    $("#quit").click(function (event) {
        switchoverColor(this);
        if (confirm("您确定要退出吗？"))
            window.location.href = "../logout";
        event.preventDefault();   //
        $(this).css("background-color", "rgba(0, 150, 255, 0.8)").parent().css("color", "#fff");
    });

    // //默认功能
    // $("#demoText1").ymdateplugin();
    // //显示其他月
    // $("#demoText2").ymdateplugin({
    //     showOtherMonths: true,//显示其他月
    //     selectOtherMonths: true//是否可选
    // });
    // /*::注意：：
    //  * 	showTimePanel和showButtonPanel只能配置一项true
    //  */
    // //显示今天/关闭
    // $("#demoText3").ymdateplugin({
    //     showButtonPanel: true
    // });
    // //显示时间的选择
    // $("#demoText4").ymdateplugin({
    //     showTimePanel: true
    // });
    // //DIV展示日历 选择某一天触发事件
    // $("#demoText5").ymdateplugin({
    //     setSelectDay: function (dd, mm, yy) {
    //         alert("您选择了：" + yy + "年" + (mm + 1) + "月" + dd + "日");
    //     }
    // });
    // //下拉年月
    // $("#demoText6").ymdateplugin({
    //     changeMonth: true,
    //     changeYear: true
    // });
    // //显示多个月
    // $("#demoText7").ymdateplugin({
    //     numberOfMonths: 3,
    //     showButtonPanel: true
    // });
    // //限制可选
    // $("#demoText8").ymdateplugin({
    //     minDate: -20,
    //     maxDate: "+1M +10D"
    // });
    // //时间范围
    // $("#demoText9").ymdateplugin({
    //     defaultDate: "+1w",
    //     changeMonth: true,
    //     numberOfMonths: 3,
    //     onClose: function (selectedDate) {
    //         $("#demoText10").ymdateplugin("option", "minDate", selectedDate);
    //     }
    // });
    // $("#demoText10").ymdateplugin({
    //     defaultDate: "+1w",
    //     changeMonth: true,
    //     numberOfMonths: 3,
    //     onClose: function (selectedDate) {
    //         $("#demoText9").ymdateplugin("option", "maxDate", selectedDate);
    //     }
    // });
    // //星期序列
    // $("#demoText11").ymdateplugin({
    //     showWeek: true,
    //     firstDay: 1
    // });

});

/**
 * @param ele1  错误信息提示框元素
 * @param ele2  按钮元素
 * @param str   错误信息
 */
function isError(ele1, ele2, str) {
    $(ele1).html(str);
    $(ele1).css("color", "rgba(255,0,0,0.8)");
    $(ele2).attr("disabled", true);
}

/**
 * @param ele   点击的元素
 * @param ele1  点击ele后要显示的元素
 */
function switchoverColor(ele, ele1) {
    $(ele).css("background-color", "rgba(0, 150, 205,1)").parent().siblings().find("li").css("background-color", "rgba(0, 150, 255, 0.8)");
    $(ele).parent().css("color", "yellow").siblings().css("color", "#fff");
    $(ele1).show().siblings().hide();
}

function quit(ele) {
    if (confirm("您确定要退出吗？"))
        window.location.href = "../logout.action";
}

function quit0(ele) {
    switchoverColor(ele);
    if (confirm("您确定要退出吗？"))
        window.location.href = "../logout.action";
    $(ele).css("background-color", "rgba(0, 150, 255, 0.8)").parent().css("color", "#fff");
}

function showLocale(objD) {
    var str;
    var yy = objD.getYear();
    if (yy < 1900)
        yy = yy + 1900;

    var MM = objD.getMonth() + 1;
    if (MM < 10)
        MM = '0' + MM;

    var dd = objD.getDate();
    if (dd < 10)
        dd = '0' + dd;

    var hh = objD.getHours();
    if (hh < 10)
        hh = '0' + hh;

    var mm = objD.getMinutes();
    if (mm < 10)
        mm = '0' + mm;

    var ss = objD.getSeconds();
    if (ss < 10)
        ss = '0' + ss;

    var ww = objD.getDay();
    if (ww === 0) ww = "星期日";
    if (ww === 1) ww = "星期一";
    if (ww === 2) ww = "星期二";
    if (ww === 3) ww = "星期三";
    if (ww === 4) ww = "星期四";
    if (ww === 5) ww = "星期五";
    if (ww === 6) ww = "星期六";
    str = yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + "  " + ww;
    return (str);
}

function tick() {
    var today;
    today = new Date();
    $("#localtime").html(showLocale(today));
    window.setTimeout("tick()", 1000);
}

function stringToDate(dateStr) {
    var fullDate = dateStr.split("-");
    return new Date(fullDate[0], fullDate[1] - 1, fullDate[2], "12", "00", "00");
}

function stringToDateTime(fDateTime) {
    var dateTime = fDateTime.split(" ");
    var fullDate = dateTime[0].split("-");
    var fullTime = dateTime[1].split(":");
    return new Date(fullDate[0], fullDate[1] - 1, fullDate[2], fullTime[0], fullTime[1], fullTime[2]);
}

function checkDateTime(ele) {
    var dateStr = $(ele).val();
    console.log("dateStr = " + dateStr);
    var dateTime12;
    if (dateStr.length < 12)
        dateTime12 = stringToDate(dateStr);
    if (dateStr.length > 12)
        dateTime12 = stringToDateTime(dateStr);

    var now = new Date();
    var time1 = dateTime12.getTime();
    var time2 = now.getTime();
    var c = 12 * 60 * 60 * 1000;
    var t = time1 - time2;
    if (t - c <= 0) {
        alert("开始时间要晚于当前时间12小时，以便相关人员做出安排！");
        $(ele).val("");
        return false;
    }
    return true;
}

// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子：
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function selectYMD(year, month, day) {
    var maxYear = new Date().getFullYear();
    var maxMonth = new Date().getMonth() + 1;

    $(month).html("");
    $(year).html("");
    for (var y = 2010; y <= maxYear; y++)
        appendOptionNum($(year), y);

    for (var m = 1; m <= 12; m++)
        appendOptionNum($(month), m);

    $(year).change(function () {
        if ($(year).val() == maxYear) {
            $(month).html("");
            for (var m = 1; m <= maxMonth; m++)
                appendOptionNum($(month), m);
        } else {
            for (var m = 1; m <= 12; m++)
                appendOptionNum($(month), m);
        }
        append_day(year, month, day);
    });

    $(month).change(function () {
        append_day(year, month, day);
    });

}

function append_day(year, month, day) {
    var m = $(month).val();
    var y = $(year).val();
    var maxYear = new Date().getFullYear();
    var maxMonth = new Date().getMonth() + 1;

    if (maxMonth < 10)
        maxMonth = "0" + maxMonth;

    var maxDay = new Date().getDate();

    var $day = $(day);
    $day.html("<option value=''>日期</option>");

    console.log("append_day---year = " + y + "---month = " + m);
    if (y == maxYear && m == maxMonth) {
        switch (m) {
            case '01':  //不能使用case 1：获得的m值为字符格式
            case '03':
            case '05':
            case '07':
            case '08':
            case '10':
            case '12':
                for (var i = 1; i <= maxDay; i++)
                    appendOptionNum($day, i);
                break;

            case '02':
                if ((y % 4 === 0 && y % 100 !== 0) || (y % 100 === 0 && y % 400 === 0))
                    for (var j = 1; j <= maxDay; j++)
                        appendOptionNum($day, j);
                else
                    for (var s = 1; s <= maxDay; s++)
                        appendOptionNum($day, s);
                break;

            case '04':
            case '06':
            case '09':
            case '11':
                for (var x = 1; x <= maxDay; x++)
                    appendOptionNum($day, x);
                break;
        }
        return;
    }
    switch (m) {
        case '01':  //不能使用case 1：获得的m值为字符格式
        case '03':
        case '05':
        case '07':
        case '08':
        case '10':
        case '12':
            for (var p = 1; p <= 31; p++)
                appendOptionNum($day, p);
            break;

        case '02':
            if ((y % 4 === 0 && y % 100 !== 0) || (y % 100 === 0 && y % 400 === 0))
                for (var q = 1; q <= 29; q++)
                    appendOptionNum($day, q);
            else
                for (var r = 1; r <= 28; r++)
                    appendOptionNum($day, r);
            break;

        case '04':
        case '06':
        case '09':
        case '11':
            for (var k = 1; k <= 30; k++)
                appendOptionNum($day, k);
            break;
    }
}

function appendOptionNum(ele, num) {
    if (num < 10)
        $(ele).append("<option value='0" + num + "'>0" + num + "</option>");
    else
        $(ele).append("<option value='" + num + "'>" + num + "</option>");
}

function choosePosition1() {
    var sectionId = $("#employee_sections").val();
    var $sp = $("#employee_positions");

    if (sectionId !== "") {
        $.post("../admin/positions",
            {"sId": sectionId},
            function (data) {
                if ($.isEmptyObject(data)) {
                    $sp.html("").append("<option value=''>职位</option>");
                } else {
                    $sp.html("<option value=''>职位</option>");
                    for (var i = 0, n = data.length; i < n; i++) {
                        $sp.append("<option value='" + data[i].id + "'>" + data[i].position + "</option>")
                    }
                }
            });
        $sp.show();
    } else
        $("#employee_positions").hide();
}

function goBack(hideEle, colorEle) {
    $(hideEle).hide();
    $(colorEle).css("background-color", "rgba(0, 150, 255, 0.8)").parent().css("color", "#fff");
}